package com.chromatic.competition.view.notifications;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.chromatic.competition.R;
import com.chromatic.competition.service.ChatBotTaskService;
import com.chromatic.competition.view.account.LoginActivity;

public class SpeakActivity extends AppCompatActivity {
    private EditText inputBox;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 检查用户是否已登录
        SharedPreferences prefs = getSharedPreferences("login_info", MODE_PRIVATE);
        String user = prefs.getString("user", "");
        String token = prefs.getString("token", "");

        if (user.equals("") || token.equals("")) {
            // 用户未登录，跳转到登录页面
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        // 用户已登录，显示MainActivity
        setContentView(R.layout.activity_speak);
        LinearLayout chatBox = findViewById(R.id.chat_box);
        inputBox = findViewById(R.id.input);
        Button sendBtn = findViewById(R.id.send_btn);

        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });

        inputBox.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    sendMessage();
                    return true;
                }
                return false;
            }
        });

        ImageView backButton = findViewById(R.id.backButton);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed(); // 调用系统返回操作
            }
        });
    }
    private void sendMessage() {
        String message = inputBox.getText().toString().trim();
        if (message.isEmpty()) {
            return;
        }
        LinearLayout chatBox = findViewById(R.id.chat_box);
        @SuppressLint("InflateParams") View messageView = getLayoutInflater().inflate(R.layout.message_self, null);
        TextView messageContent = messageView.findViewById(R.id.message_content);
        messageContent.setText(message);
        // 将新的消息视图添加到聊天框中
        chatBox.addView(messageView);
        // 滚动聊天框以便最新消息可见
        ScrollView chatBox_Scroll = findViewById(R.id.chat_box_scrollview);
        chatBox_Scroll.fullScroll(View.FOCUS_DOWN);
        inputBox.setText("");
        // 调用 AsyncTask
        new ChatBotTaskService(SpeakActivity.this).execute(message);
    }
}